"use strict";

/**
 * 文件列表控制器
 */
angular.module("app").controller("articlePublishCtrl", function($scope, $http, $location) {	

	$scope.article_content_rest_length = 300;	
	$scope.publish_content_area_keyup = function($event) {
		var article_textarea = $($event.target);
		var len = article_textarea.val().length;
		if( len > 300){
			article_textarea.val(article_textarea.val().substring(0,300));
		}
		$scope.article_content_rest_length = 300 - len;
	};
	
	$scope.do_publish = function () {
		var url = $("#article_form").attr("action");
	    $http({
	    	url : url,
	    	method : "POST",
	    	headers : {
	    		"Content-Type" : undefined
	    	},
	    	transformRequest : function(data) {
	    	    return new FormData($("#article_form")[0]);
	    	}
	    }).success(function(d) {
	    	if(d.success){
	    		var textarea = $("#article_form").find("textarea[name='content']");
	    		var f = $("#article_form").find("input[name='pic']");
	    		
	    		textarea.val("");	    		
	    		f.after(f.clone().val(""));      
	    		f.remove();
	    		
	    		$location.path("/article/publish/result/true");
			} else if(d.retCode == "NO_SESSION"){
				//$location.path("/user/signin");
				$('#login_modal').modal("show");
			}
	    });
	};
	
})